package com.hnx.admin.aurora.system.service.impl;

import com.hnx.admin.aurora.annotation.Pager;
import com.hnx.admin.aurora.api.cache.CacheAPI;
import com.hnx.admin.aurora.redis.RedisCache;
import com.hnx.admin.aurora.system.domain.convert.dict.DictConvert;
import com.hnx.admin.aurora.system.domain.dto.dict.entity.DictDataDTO;
import com.hnx.admin.aurora.system.domain.param.dict.form.DictDataForm;
import com.hnx.admin.aurora.system.domain.param.dict.query.DictDataQuery;
import com.hnx.admin.aurora.system.domain.vo.dict.entity.DictDataVO;
import com.hnx.admin.aurora.system.mapper.dict.DictDataMapper;
import com.hnx.admin.aurora.system.service.SysDictDataService;
import com.hnx.admin.aurora.system.service.SysDictTypeService;
import com.hnx.admin.aurora.web.json.ResultJson;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/*
创建人： 倔强的头发
创建时间：2025/6/18
功能描述:

*/
@Service
@RequiredArgsConstructor
public class SysDictDataServiceImpl implements SysDictDataService, CacheAPI {
    private final SysDictTypeService sysDictTypeService;
    private final DictDataMapper dictDataMapper;
    private final DictConvert dictConvert;
    private final RedisCache redisCache;

    @Override
    public ResultJson selectDictDataByType(String dictType) {
        List<DictDataVO> list = redisCache.getCacheList(getDictKey(dictType), DictDataVO.class).orElseGet(() -> selectType(dictType));
        return ResultJson.data(list);
    }

    @Override
    @Pager
    public ResultJson selectDictDataList(DictDataQuery dictData) {
        List<DictDataDTO> list = dictDataMapper.selectDictDataList(dictData);
        return ResultJson.rows(list);
    }

    @Override
    public ResultJson insertDictData(DictDataForm dict) {
        int row = dictDataMapper.insertDictData(dict);
        redisCache.delete(getDictKey(dict.getDictType()));
        return ResultJson.condition(row);
    }

    @Override
    public ResultJson deleteDictDataByIds(String[] dictCodes) {
        for (String dictCode : dictCodes)
        {
            DictDataDTO data = (DictDataDTO) selectDictDataById(dictCode).get("data");
            dictDataMapper.deleteDictDataById(dictCode);
            redisCache.delete(getDictKey(data.getDictType()));
        }
        return ResultJson.success();
    }

    @Override
    public ResultJson selectDictDataById(String dictCode) {
        DictDataDTO dictDataDTO = dictDataMapper.selectDictDataById(dictCode);
        return ResultJson.data(dictDataDTO);
    }

    @Override
    public ResultJson updateDictData(DictDataForm dict) {
        int row = dictDataMapper.updateDictData(dict);
        redisCache.delete(getDictKey(dict.getDictType()));
        return ResultJson.condition(row);
    }

    private List<DictDataVO> selectType(String dictType) {
        List<DictDataDTO> data = dictDataMapper.selectDictDataByType(dictType);
        if (Objects.nonNull(data)) {
            List<DictDataVO> cacheList = dictConvert.toList(data);
            redisCache.setCacheList(getDictKey(dictType), cacheList);
            return cacheList;
        }
        return null;
    }
}
